﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ECRMS.WinForm.BaseClass;
using System.Linq;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using ECRMS.WinForm.BaseClass;
using ECRMS.WinForm.BaseClass.Common;
using ECRMS.WinForm.Properties;
using ECRMS.WinForm.WinService;
using ECRMS.WinForm.ModuleEdits;
using System.Linq;
using ECRMS.WinForm.BaseClass.Other;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;

namespace ECRMS.WinForm.Modules
{
        [ModuleInstance((int)ModuleEnum.公司新闻)]
    public partial class CampanyInfoModelu : BaseModule
    {
        public CampanyInfoModelu()
        {
            base.core.CreateWaitDialog("初始化中请稍候...", "公司新闻");
            InitializeComponent();
            InitView();
            InitData();
            InitEventHandler();
            base.core.CloseWaitDialog();
        }

        private void InitEventHandler()
        {
            //语言切换
            base.core.LangChanged += new Core.LanguageChangedHandler(this.ChangeLanguage);
        }

        private void ChangeLanguage(LangEnum lang)
        {
            //switch (core.NowLanguage)
            switch (lang)
            {
                case LangEnum.China:
                case LangEnum.中文:

                    break;
                case LangEnum.English:
                case LangEnum.英文:

                    break;
                default:
                    break;
            }
        }

        private void InitData(bool isPostBack = false)
        {
            if (!isPostBack)
            {
                FuncResult result = core.CorePost.GetPosts(core.CoreUser.UserInfo.CorporationRefId);
                if (result.Status == 0)
                {
                    XtraMessageBox.Show(this, Language_ch.ResourceManager.GetString(result.Message));
                    return;
                }
            }
            this.gdPost.DataSource = core.CorePost.Posts;
            this.gdPost.RefreshDataSource();

            BindContent();
        }

        private void BindContent()
        {
            Guid currentRowIndex = core.CoreCommon.GetGridFocusRowRefId(gdPost, gridView1, "PostRefId");
            recPost.WordMLText = System.Text.Encoding.UTF8.GetString(core.CorePost.Posts.FirstOrDefault(p => p.PostRefId == currentRowIndex).Content.ToArray());
        }

        private void InitView()
        {
            if (core.CoreUser.UserInfo.UserTypeId == (int)AuthorityEnum.CorporationClientAdmin)
            {
                MenuBarVisiable = true;
                base.otherBarButton1Visiable = false;
                base.otherBarButton2Visiable = false;
                base.otherBarButton3Visiable = false;
                base.outBarButtonVisiable = false;
            }
            else
            {
                MenuBarVisiable = false;
            }
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            BindContent();
        }

        //新增
        public override void newBarButton_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            base.newBarButton_ItemClick(sender, e);

            core.CorePost.OperatorRow = new CtPost();
            core.CorePost.OperatorRow.CategoryId = (int)PostType.CropInfo;
            core.CorePost.OperatorRow.CreateUserRefId = core.CoreUser.UserInfo.UserRefId;
            core.CorePost.OperatorRow.PostRefId = Guid.NewGuid();

            PostEdit postEdit = new PostEdit(this.RectangleToScreen(Bounds), OperationEnum.New);
            DialogResult result = postEdit.ShowDialog();

            if (result == DialogResult.OK)
            {
                InitData();
            }

            core.CorePost.OperatorRow = null;
            postEdit.Dispose();

        }

        //修改
        public override void editBarButton_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            base.editBarButton_ItemClick(sender, e);
            Guid rowIndex = core.CoreCommon.GetGridFocusRowRefId(gdPost, gridView1, "PostRefId");
            if (rowIndex == Guid.Empty)
            {
                return;
            }
            CtPost post = core.CorePost.Posts.FirstOrDefault(p => p.PostRefId == rowIndex);

            core.CorePost.OperatorRow = post;

            PostEdit postEdit = new PostEdit(this.RectangleToScreen(Bounds), OperationEnum.Modify);
            DialogResult result = postEdit.ShowDialog();

            if (result == DialogResult.OK)
            {
                //数据量不大 所以去服务端获取一遍
                InitData();
            }

            core.CorePost.OperatorRow = null;
            postEdit.Dispose();

        }

        public override void delBarButton_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            base.delBarButton_ItemClick(sender, e);
            Guid rowIndex = core.CoreCommon.GetGridFocusRowRefId(gdPost, gridView1, "PostRefId");
            if (rowIndex == Guid.Empty)
            {
                return;
            }
            CtPost post = core.CorePost.Posts.FirstOrDefault(p => p.PostRefId == rowIndex);
            core.CorePost.OperatorRow = post;

            DialogResult result = XtraMessageBox.Show(this, "确定删除这篇文章？", "删除提醒", MessageBoxButtons.YesNo);

            if (result == DialogResult.No)
            {
                core.CorePost.OperatorRow = null;
                return;
            }
            else
            {
                core.CorePost.OperatorRow.IsDisabled = true;
                FuncResult result1 = core.CorePost.UpdatePost();
                if (result1.Status == 0)
                {
                    XtraMessageBox.Show(this, Language_ch.ResourceManager.GetString(result1.Message));
                    core.CorePost.OperatorRow = null;
                    return;
                }
                //数据量不大 所以去服务端获取一遍
                InitData();
            }
        }
    }
}
